/* ============================================================================
* 功能描述 ：主函数
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/


#include "main.h"
bit time_f;
char alarm_temp;
bit buzz_on;
bit sleep_flag;
bit alarm_io_f;
static unsigned long now_time_f;
static unsigned long last_time_f;
char io_delay;
bit remove_alarm;
bit buzz_kg =0;
bit delay_io_alarm;
bit led_switch;
static unsigned long led_switch_time;
static unsigned long sleep_time;

char sj = 1;
char ch;

bit key_flag;

bit RXOK_f;

void _data_tx_pack(void)
{
	char ap_data[10];
	sj ++;
	ap_data[0] = 0x01;
	ap_data[1] = 0x02;
	ap_data[2] = 0x03;
	ap_data[3] = 0x04;
	ap_data[4] = 0x05;
	ap_data[5] = 0x06;
	ap_data[6] = sj;
	ap_data[7] = 0x06;
	ap_data[8] = 0x06;
	Uart_Tx_buf(ap_data, 9);
	
	if(sj == 100)
	{
		sj = 0;
	}
	

}


void main(void)
{
	char i;
	Init_System();			// 系统初始化		
	

	Uart_Init();			// 串口初始化
#if 1	
	Time1_Init();			// 时钟1初始化
	
	ADC_INIT();				// adc初始化
	Hly_Water_Immersion_Init();
	
	Zigbee_wake_up_init();		// zigbee模块唤醒
	
	

//	app_init();
	Zigbee_Led_init();
	hly_pwm_init();

	
	hly_Obtain_voltage();
	now_time_f = 0;
	key_flag = 0;
	last_time_f = 0;
	time_f = 0;
//	Hly_Adc_update();
	Zigbee_App_Init();		// 初始化zigbee参数
	ch = 0;
	delay_io_alarm = 0;
	buzz_on = 0;
	sleep_flag = 0;
	io_delay = 0;
	led_switch = 1;
	xx_f = 0;

	remove_alarm = 0;

	app_data.zong_time = 0;
	Zigbee_led_off();
#endif
	while(1)
	{
		//	RB5 = 0;
		asm("clrwdt");	// 喂狗
	//	Zigbee_led_on();
		Zigbee_Cmd_handle();	// 串口数据处理

		App_Run();   // 应用运行！
			
			
		
		
	
	
#if 1
		ch = Hly_Obtain_Water_val(); // 获取水浸值
		if(led_switch != alarm_io_f || buzz_on == 1)
		{
			
			sleep_flag = 1;
			Zigbee_led_on();
			if(hly_get_elapsed_ms(led_switch_time) >= 33)
			{
				led_switch_time = get_current_ms();
				Zigbee_led_off();
				led_switch = alarm_io_f;
				buzz_on = 0;
			}
			
		}

		
		if(ch == 1)
		{
		
			DelayXms(20);
			if(ch == 1)
			{
				if(delay_io_alarm == 0)
				{
					alarm_io_f = 1;
					Zigbee_wake_up();
					DelayXms(15);
					Hly_zigbee_water_alarm();
					Zigbee_sleep();
				}
				if(hly_get_elapsed_s(app_data.alarm_io_time) >= 24)
				{
					app_data.alarm_io_time = get_current_s();
					delay_io_alarm = 0;
					io_delay++;
				}
				
				if(io_delay == 60)
				{
					io_delay = 0;
					Zigbee_wake_up();
					DelayXms(15);
					hly_Obtain_voltage();
					Hly_tuya_updata(Report_specified_type, app_data.power_supply);
				  
					
					Zigbee_sleep();
						

				}
			}
			
		}
		else if(ch == 0)
		{
			DelayXms(20);
			if(ch == 0)
			{
				PWM1EN = 0;
				delay_io_alarm = 0;
				remove_alarm = 0;
				if(alarm_io_f == 1)
				{
					Zigbee_wake_up();
					DelayXms(15);
					Hly_zigbee_water_normal();
					alarm_io_f = 0;
					Zigbee_sleep();
				}
			}
		}
		
		
		if(alarm_io_f == 1 && remove_alarm == 0)
		{
			delay_io_alarm = 1;
			sleep_flag = 1;
			if(hly_get_elapsed_ms(app_data.buzz_time) >= 5)
			{
				app_data.buzz_time = get_current_ms();
				if(buzz_kg == 0)
				{
					CLKDIV2 = 1;
					CLKDIV1 = 1;
					CLKDIV0 = 0;
					PWM1EN = 1;
					
					PWMCON0 = 0B11000010;
					buzz_kg = 1;
				}
				else if(buzz_kg == 1)
				{
					PWMCON0 = 0B11000000;
					buzz_kg = 0;
					alarm_temp ++;
				}
	
			}
			if(alarm_temp == 66)
			{
				alarm_io_f = 0;
				alarm_temp = 0;
			}
			
		}
		else if(remove_alarm == 1)
		{
			PWMCON0 = 0B11000000;
		}
		//key_on();
		
	//	Zigbee_sleep();
	//	DelayXms(200);
		
	
		
	
		
			
	



	
		if(sleep_flag == 1 )
		{
			if(hly_get_elapsed_ms(sleep_time) >= 66)
			{
				sleep_flag = 0;
				sleep_time = get_current_ms();
			}
		
		}
	
		if(sleep_flag == 0 && alarm_io_f == 0 && key_flag == 0) 
		{
			Zigbee_led_off();
			PWM1EN = 0;
		
			Sleep_Mode();

		}
		
			
#endif	

		

		

		
#if 0
		if(RXOK_f==1)
		{
			for(i=0;i<8;i++)
			{
					
				TXREG =RxTable[i];

				while(TRMT==0);
			}

			RXOK_f = 0;
		}
#endif
	}
}
void key_on(void)
{
//	HLY_GetTick();
	if(RB4 == 0)
	{
		DelayXms(10);  // 防抖


		if(RB4 == 0)
		{
			if(key_flag == 0)
			{
				last_time_f = now_time_f;
				time_f = 1;
			}
			if(abs(now_time_f - last_time_f) == 166 && time_f == 1)
			{
				Zigbee_wake_up();
				DelayXms(30);
				sleep_flag = 1;
				Zigbee_Network_Status();				
				Zigbee_sleep();
				time_f = 0;
			}
			key_flag = 1;
		}
		
	}
	else
	{
		DelayXms(10);        
		if(RB4 == 1 && key_flag == 1 )   //检测弹起
		{
			//_data_tx_pack();             //弹起后处理任务
			if(time_f == 1)
			{
				remove_alarm = 1;
				Zigbee_wake_up();
				DelayXms(30);
				Hly_Packet_Network_msg(query_current_network_status, 0, 0);
				Zigbee_sleep();
				//DelayXms(15);
				time_f = 0;
				sleep_flag = 1;
				
				buzz_on = 1;

			}
			time_f = 0;
			key_flag = 0;
		
			
		}			
	}
}

unsigned long get_current_ms(void)
{
    return now_time_f;
}

unsigned long get_current_s(void)
{
    return app_data.zong_time;
}


/***********************************************
函数名称：Init_System
函数功能：系统初始化
入口参数：无
出口参数：无
备注：
************************************************/
void Init_System()
{
	asm("nop");
	asm("clrwdt");
	
	INTCON = 0x0c;					//系统初始化
	OSCCON = 0B01100010;			 // 看门狗配置
	OPTION_REG = 0B00001111;		// 看门狗配置

	

	TRISC = 0;
	PORTC = 0;
	WPUC = 0;

	
	TRISB = 0;
	PORTB = 0;
	WPDB = 0;
	WPUB = 0;
	ODCONB = 0;
	IOCB = 0;

	
	TRISA = 0;
	PORTA = 0;
	WPUA = 0;
	WPDA = 0;
	ODCONA = 0;
	IOCA = 0;
	
	TRISB3 = 0; // 电源检测开关
		
	/* 按键io初始化 */
	TRISB4 = 1 ;
	WPUB4 = 1;
	RB4 = 1;
	IOCB4 = 1;

	/* 唤醒muc io初始化 */
	TRISA5 = 1 ;
	WPUA5 = 1;
	IOCA5 = 1;
	RAIE = 1;	// 允许pb中断
	RBIE = 1;	// 允许pb中断

	PEIE = 1;
	RCIE = 1;
	GIE  = 1;
	


}


/***********************************************
函数名称：Time1_Init
函数功能：time1初始化
入口参数：无
出口参数：无
备注：
************************************************/

void Time1_Init()
{
	
	
	T2CON = 0B01111110;
	TMR2 = 0;
	PR2 = 0xff;

	TMR2IE = 1;

	
}

void Sleep_Mode()
{
	#if 1
	
	app_data.zong_time++;
	if(app_data.zong_time >= 3600)
	{
		app_data.zong_time = 0;
	}
	
	EECON1 = 0;					//清此寄存器,有好处



	ADCON0 = 0;					//关闭所有模块
	PWMCON0 = 0;

	
	IOCA = 0B00100000;			//允许RA5的IO口电平变化中断
	IOCB = 0B10010000;			//允许RB4, RB7的IO口电平变化中断
	RBIE = 1;					//允许PORTB电平变化中断
	RAIE = 1;					//允许PORTA电平变化中断
	RCIE = 1;
	
	GIE = 1;					//GIE = 0时，唤醒后执行SLEEP后程序;GIE = 1时，唤醒后跳至中断服务
	
	
	PORTB;						//读PORTB值并锁存		
	PORTA;
	asm("sleep");				//进入休眠模式
	
	asm("nop");
	//Init_System();
	#endif
}



/***********************************************
函数名称：PB_Isr
函数功能：PB电平变化中断服务
入口参数：无
出口参数：无
备注：
************************************************/
void interrupt PB_Isr()
{
	
	

	if(RBIF)
	{
		RBIF = 0;			//清中断标志
		//RB6 = ~RB6;
		PORTA;
		
	
	}
	if(RAIF)
	{
		RAIF = 0;			//清中断标志
		sleep_flag = 1;
		//RB6 = ~RB6;
		PORTA;
	}
	
	if (TMR2IF)
	{
		TMR2 = 0;
		TMR2IF = 0;
		now_time_f++;
		if(now_time_f == 50000)
		{
			now_time_f = 0;
		}
	}

#if 0	
	if(RCIF==1) 				// 接收中断
	{
		RCIF = 0;
	//	_data_tx_pack();
	}

#else
	static unsigned char RxNum=0,TEMP;

	if(RCIF==1)					// 接收中断
	{
	    
		uart_cmd_rx_cplt_callback();
		
	}
#endif
}

